package mrHunter.puzzles.array;

public class Puzzle1 {
    //Puzzle1
    // 题目名称: 1 # Binary Search on Sorted Array [有序数组的二分查找]
    public String binarySearch(String input){
        //int[] A, int n, int x
        //int y
        String[] lines = input.split(",");
        int n = Integer.parseInt(lines[1]);
        int x = Integer.parseInt(lines[2]);
        int[] A = new int[n];
        String[] nums = lines[0].split(" ");
        for (int i = 0; i < n; i++) A[i] = Integer.parseInt(nums[i]);

        int low = 0, high = n, mid;
        int res = -1;
        while(low <= high){
            mid = low + (high - low) / 2;
            if(mid<n&&A[mid] == x){
                res = mid;
                break;
            }else if(mid<n&&A[mid] > x){
                high = mid - 1;
            }else{
                low = mid + 1;
            }
        }
        return "" + res;
    }

//     public static void main(String[] args) {
//       Puzzle1 pz = new Puzzle1();
//       System.out.println(pz.binarySearch("1,2,3 3 2"));
//     }
}
